<?php

class Stat_aftersaleApp extends BackendApp {

    var $_refund_mod;

    function __construct() {
        $this->Stat_aftersaleApp();
    }

    function Stat_aftersaleApp() {
        parent::__construct();
        $this->_refund_mod = &m('refund');
        import('datehelper.lib');
        import('statistics.lib');
        $this->import_resource(
                array(
                    'script' => 'highcharts/highcharts.js,statistics.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                    'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css')
        );
        $this->search_arr = $_REQUEST;
        //处理搜索时间
        if (in_array($_REQUEST['act'], array('refund'))) {
            $this->search_arr = dealwithSearchTime($this->search_arr);
            //获得系统年份
            $year_arr = getSystemYearArr();
            //获得系统月份
            $month_arr = getSystemMonthArr();
            //获得本月的周时间段
            $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
            $this->assign('year_arr', $year_arr);
            $this->assign('month_arr', $month_arr);
            $this->assign('week_arr', $week_arr);
        }
        $this->assign('actionurl', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
        $this->assign('search_arr', $this->search_arr);
    }

    function refund() {
        if (!$this->search_arr['search_type']) {
            $this->search_arr['search_type'] = 'day';
        }

        //获得搜索的开始时间和结束时间
        $searchtime_arr = getStarttimeAndEndtime($this->search_arr);

        $field = ' SUM(refund_goods_fee+refund_shipping_fee) as amount ';
        if ($this->search_arr['search_type'] == 'day') {
            //构造横轴数据
            for ($i = 0; $i < 24; $i++) {
                $stat_arr['xAxis']['categories'][] = "$i";
                $statlist[$i] = 0;
            }
            $field .= ' ,HOUR(FROM_UNIXTIME(created)) as timeval ';
        }
        if ($this->search_arr['search_type'] == 'week') {
            //构造横轴数据
            for ($i = 1; $i <= 7; $i++) {
                $tmp_weekarr = getSystemWeekArr();
                //横轴
                $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
                unset($tmp_weekarr);
                $statlist[$i] = 0;
            }
            $field .= ' ,WEEKDAY(FROM_UNIXTIME(created))+1 as timeval ';
        }
        if ($this->search_arr['search_type'] == 'month') {
            //计算横轴的最大量（由于每个月的天数不同）
            $dayofmonth = date('t', $searchtime_arr[0]);
            //构造横轴数据
            for ($i = 1; $i <= $dayofmonth; $i++) {
                //横轴
                $stat_arr['xAxis']['categories'][] = $i;
                $statlist[$i] = 0;
            }
            $field .= ' ,day(FROM_UNIXTIME(created)) as timeval ';
        }

        $statlist_tmp = $this->_refund_mod->find(array(
            'fields' => $field,
            'conditions' => "created>{$searchtime_arr[0]} AND created < {$searchtime_arr[1]} group by timeval",
            'order' => 'timeval asc',
        ));

        if ($statlist_tmp) {
            foreach ((array) $statlist_tmp as $k => $v) {
                $statlist[$v['timeval']] = floatval($v['amount']);
            }
        }

        //得到统计图数据
        $stat_arr['legend']['enabled'] = false;
        $stat_arr['series'][0]['name'] = '退款金额';
        $stat_arr['series'][0]['data'] = array_values($statlist);
        $stat_arr['title'] = '退款金额统计';
        $stat_arr['yAxis'] = '金额';
        $stat_json = getStatData_LineLabels($stat_arr);
        $this->assign('stat_json', $stat_json);
        $this->assign('searchtime', implode('|', $searchtime_arr));
        $this->_curmenu('stat_aftersale_refund');
        $this->assign('actionurl', "index.php?app={$this->search_arr['app']}&act=refundlist&t=".implode('|', $searchtime_arr));
        $this->display('stat_aftersale.refund.html');
    }

    public function refundlist() {
        $searchtime_arr = explode('|', $this->search_arr['t']);
        if ($this->search_arr['exporttype'] == 'excel') {
            $refundlist_tmp = $this->_refund_mod->find(array(
                'conditions' => "created>{$searchtime_arr[0]} AND created < {$searchtime_arr[1]}",
                'order' => 'refund_id desc',
                'join' => 'belongs_to_order',
            ));
        } else {
            $refundstate_arr = array(
                'CLOSED'=>'退款关闭',
                'SUCCESS'=>'退款成功',
                'FAILED'=>'退款失败',
                'WAIT_SELLER_AGREE'=>'买家已经申请退款，等待卖家同意',
                'SELLER_REFUSE_BUYER'=>'卖家拒绝退款，等待买家修改中',
                'WAIT_ADMIN_AGREE'=>'卖家已经同意退款，等待管理员审核',
                'WAIT_SELLER_CONFIRM'=>'退款申请等待卖家确认中',
            );
            $page = $this->_get_page(10);
            $refundlist_tmp = $this->_refund_mod->find(array(
                'fields' => 'this.*,seller_name,buyer_name',
                'conditions' => "created>{$searchtime_arr[0]} AND created < {$searchtime_arr[1]}",
                'order' => 'refund_id desc',
                'limit' => $page['limit'],
                'join' => 'belongs_to_order',
                'count' => true,
            ));
            $page['item_count'] = $this->_refund_mod->getCount();
            $this->_format_page($page);
            $this->assign('page_info', $page);
        }
        $statheader = array();
        $statheader[] = array('text' => '订单编号', 'key' => 'order_id');
        $statheader[] = array('text' => '退款编号', 'key' => 'refund_sn');
        $statheader[] = array('text' => '店铺名', 'key' => 'seller_name');
        $statheader[] = array('text' => '商品ID', 'key' => 'goods_id');
        $statheader[] = array('text' => '买家会员名', 'key' => 'buyer_name');
        $statheader[] = array('text' => '申请时间', 'key' => 'created');
        $statheader[] = array('text' => '退款邮费', 'key' => 'refund_shipping_fee');
        $statheader[] = array('text' => '退款产品金额', 'key' => 'refund_goods_fee');
        $statheader[] = array('text' => '退款状态', 'key' => 'status');
        foreach ((array) $refundlist_tmp as $k => $v) {
            $tmp = $v;
            $tmp['created'] = @date('Y-m-d H:i:s', $v['created']);
            $tmp['status']= $refundstate_arr[$tmp['status']];
            $statlist[] = $tmp;
        }
        if ($this->search_arr['exporttype'] == 'excel') {
            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            foreach ((array) $statheader as $k => $v) {
                $excel_data[0][] = array('styleid' => 's_title', 'data' => $v['text']);
            }
            //data
            foreach ((array) $statlist as $k => $v) {
                foreach ((array) $statheader as $h_k => $h_v) {
                    $excel_data[$k + 1][] = array('data' => $v[$h_v['key']]);
                }
            }
            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('退款记录', CHARSET));
            $excel_obj->generateXML($excel_obj->charset('退款记录', CHARSET) . date('Y-m-d-H', time()));
            exit();
        } else {
            $this->assign('statheader', $statheader);
            $this->assign('statlist', $statlist);
            $this->assign('searchtime', $_GET['t']);
            $this->display('stat_aftersale.refundlist.html');
        }
    }

    function _get_member_submenu() {
        $submenus = array(
            array(
                'name' => 'stat_aftersale_refund',
                'url' => 'index.php?app=stat_member&amp;act=refund',
            ),
        );
        return $submenus;
    }

}

?>
